package com.lightsaber.trade.cloth.service.planning.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.entity.AuditedObject;
import com.lightsaber.trade.core.entity.BaseEntity;
import com.lightsaber.trade.core.service.HppBaseServiceImpl;
import com.lightsaber.trade.cloth.dao.planning.ProgrammePlanningItemDao;
import com.lightsaber.trade.cloth.entity.planning.ProgrammePlanningItem;
import com.lightsaber.trade.cloth.service.planning.ProgrammePlanningItemService;
import com.lightsaber.trade.cloth.service.programme.OfferingCampusManager;

@Service
@Component
@Transactional
public class ProgrammePlanningItemServiceImpl extends
        HppBaseServiceImpl<ProgrammePlanningItem, String> implements ProgrammePlanningItemService {

    @Autowired
    private ProgrammePlanningItemDao programmePlanningItemDao;

    @Override
    protected BaseDao<ProgrammePlanningItem, String> getDao() {
        return programmePlanningItemDao;
    }

    @Autowired
    OfferingCampusManager campusManager;

    @Override
    public void save(final ProgrammePlanningItem programmePlanningItem) {
        if (StringUtils.isBlank(programmePlanningItem.getId())) {
            programmePlanningItemDao.save(programmePlanningItem);
        } else {
            ProgrammePlanningItem old = get(programmePlanningItem.getId());
            BeanUtils.copyProperties(programmePlanningItem, old, new String[] { BaseEntity.ID,
                    BaseEntity.VERSION, "level2Planning", AuditedObject.CREATED_BY,
                    AuditedObject.CREATED_DATE, "offeringCampus" });
            programmePlanningItemDao.update(old);
        }

    }

    @Override
    public ProgrammePlanningItem findUniquePpi(final String level2PlanningId, final String code,
            final Integer planYear, final Integer studyYear, final String operatingCampus,
            final String operatingUnit) {
        return programmePlanningItemDao.findUniquePpi(level2PlanningId, code, planYear, studyYear,
                operatingCampus, operatingUnit);
    }

    @Override
    public List<Long> sumForPlannedPlaceByLevel2IdAndCampusIdAndPlanYear(
            final String level2PlanningId, final String campusId, final Integer planYear) {
        return programmePlanningItemDao.sumForPlannedPlaceByLevel2IdAndCampusIdAndPlanYear(
                level2PlanningId, campusId, planYear);
    }

}
